package com.qf.md.type.service.impl;

import com.qf.entity.md.type.MdItemType;
import com.qf.exception.CustomerException;
import com.qf.md.type.dao.MdItemTypeDAO;
import com.qf.md.type.param.TypeParam;
import com.qf.md.type.service.MdItemService;
import com.qf.md.type.vo.MdTypeVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("mit")
public class MdItemServiceImpl implements MdItemService {
    @Resource
    private MdItemTypeDAO mdItemTypeDAO;
    @Override
    public List<MdTypeVO> findTypeVO() {
        return mdItemTypeDAO.selectTypeVO();
    }

    @Override
    public void deleteById(Long id) {
        //正常逻辑直接删除,但是需要考虑删除的id是否是父级id
        List<MdItemType> mdItemTypes = mdItemTypeDAO.selectByParentType(id);
        if(mdItemTypes !=null && mdItemTypes.size()>0){
            throw new CustomerException("5003","删除失败该分类有子类,请先删除子类");

        }
        mdItemTypeDAO.deleteById(id);

    }

    @Override
    public MdItemType typeInfo(Long id) {

        return mdItemTypeDAO.selectById(id);
    }

    @Override
    public void save(TypeParam typeParam) {
        System.out.println(typeParam.getAncestors());
        MdItemType mdItemType=mdItemTypeDAO.selectById(typeParam.getParentTypeId());
        //前端传过来的对象 现在只需要处理一个字段
        String ancestors = mdItemType.getAncestors();
        typeParam.setAncestors(ancestors+","+typeParam.getParentTypeId());
//        System.out.println();
        System.out.println("========================="+typeParam.getAncestors());
        mdItemTypeDAO.insertType(typeParam);

    }


}
